<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="团购名称：">
          <el-input v-model="formInline.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="推荐模块：">
          <el-select v-model="formInline.module" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="sumit_button" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" class="sumit_button" @click="addBuy">新增</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:operate="{ scope }">
          <el-button type="primary" size="small" @click="editJump(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="small" @click="deactivate(scope.row.id)">删除</el-button>
        </template>
      </ex-table>
    </div>
    <!-- 抖音小程序商品推荐 -->
    <el-dialog title="抖音小程序商品推荐" :visible.sync="dialogVisible" :close-on-click-modal="false" width="500px">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
        <el-form-item label="商家店铺名称：" prop="shopObject">
          <el-select v-model="ruleForm.shopObject" placeholder="请选择" value-key="id" filterable @change="optionChange">
            <el-option v-for="shopObject in shopOptions" :key="shopObject.id" :label="shopObject.value" :value="shopObject">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团购商品：" prop="grouponId">
          <el-select v-model="ruleForm.grouponId" placeholder="请选择" filterable>
            <el-option v-for="item in grouponOptions" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input v-model="ruleForm.sort" placeholder="请输入" style="width:203px"></el-input>
        </el-form-item>
        <el-form-item label="推荐模块：" prop="module">
          <el-select v-model="ruleForm.module" placeholder="请选择" filterable>
            <el-option v-for="item in moduleOptions" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="submitLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  // components
  import {
    adminDict,
    grouponRecommendLs,
    getShopLs,
    getGrouponList,
    addGrouponRecommend,
    editGrouponRecommend,
    getGrouponRecommend,
    deleteGrouponRecommend
  } from "@/api/operateActive";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "billList",
    components: {
      ExportExcelButton,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        loading: false,
        submitLoading: false,
        formInline: {
          name: "", //活动名称
          module: "", //推荐模块 1 爆品推荐 2 今日上新
        },
        dialogVisible: false,
        options: [], //
        moduleOptions: [], //推荐模块下拉
        shopOptions: [], //店铺下拉
        grouponOptions: [], //团购下拉
        ruleForm: {
          shopObject: {}, //店铺下拉对象，后端不做处理
          grouponId: "", //团购id
          module: "", //推荐模块 1 爆品推荐 2 今日上新  字典值查询参数 merchant_groupon_recommend_module
          shopId: "", //店铺id
          sort: "", //推荐序号
        },
        rules: {
          shopObject: [{
            required: true,
            message: "请选择店铺",
            trigger: "change"
          }],
          shopId: [{
            required: true,
            message: "请选择店铺",
            trigger: "change"
          }],
          grouponId: [{
            required: true,
            message: "请选择团购内容",
            trigger: "change"
          }],
          module: [{
            required: true,
            message: "请选择推荐模块",
            trigger: "change"
          }],
          sort: [{
            required: true,
            message: "请输入排序号",
            trigger: "blur"
          }],
        },
        tableData: [],
        tableColumn: [{
            prop: "name",
            label: "团购名称"
          },
          {
            prop: "shopName",
            label: "店铺名称"
          },
          {
            prop: "statusDict",
            label: "团购状态"
          },
          {
            prop: "moduleDict",
            label: "推荐模块"
          },
          {
            prop: "sort",
            label: "排序"
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            width: 240,
            fixed: "right",
          },
        ],
        shopId: '',
        editId: '', //编辑id
      };
    },
    computed: {
      ...mapGetters(["isChain"]),
    },
    created() {},
    mounted() {
      this.getTypeList()
      // this.getShopList()
      this.getList();
    },
    methods: {
      getTypeList() {
        //获取推荐类型
        const params = {
          dictType: "merchant_groupon_recommend_module",
        };
        adminDict(params).then((res) => {
          var arr = [{
            id: "",
            value: "全部",
          }, ];
          this.options = arr.concat(res.result[0].dictList);
          this.moduleOptions = res.result[0].dictList;
        });
      },
      getShopList() { //获取店铺
        const params = {};
        getShopLs(params).then((res) => {
          this.shopOptions = res.result;
        });
      },
      getGrouponList(shopId) { //获取团购名称
        const params = {
          merchantId: shopId
        };
        getGrouponList(params).then((res) => {
          this.grouponOptions = res.result;
        });
      },
      optionChange(e) { //店铺选择
        console.log(e);
        this.shopId = e.additionalValue
        this.ruleForm.shopId = e.id
        this.ruleForm.grouponId = ''
        this.getGrouponList(this.shopId)
        // this.ruleForm.shopName = e;
      },
      addBuy() {
        //新增
        this.getShopList()
        this.ruleForm = {
            shopObject: '', //店铺下拉对象，后端不做处理
            grouponId: "", //团购id
            module: "", //推荐模块 1 爆品推荐 2 今日上新  字典值查询参数 merchant_groupon_recommend_module
            shopId: "", //店铺id
            sort: "", //推荐序号
          },
          this.editId = null
        this.dialogVisible = true;
        this.submitLoading = false
      },
      editJump(id) {
        //编辑
        this.getShopList()
        this.editId = id
        this.getGrouponRecommend()
        this.submitLoading = false
        this.dialogVisible = true;
      },
      getGrouponRecommend() { //获取编辑详情
        var params = {
          id: this.editId
        };
        getGrouponRecommend(params).then((res) => {
          let info = res.result
          console.log(this.shopOptions,info.shopId)
          let list = this.shopOptions
          let obj = list.filter((item,index)=>{
            if(item.id==info.shopId){
              return item
            }
          })
          console.log(obj[0])
          this.ruleForm.shopObject = obj[0]
          this.getGrouponList(this.ruleForm.shopObject.additionalValue)
          this.ruleForm.grouponId = info.grouponId
          this.ruleForm.module = info.module
          this.ruleForm.shopId = info.shopId
          this.ruleForm.sort = info.sort
        })
      },
      deactivate(id) {
        //删除
         this.$confirm("是否删除？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            deleteGrouponRecommend(id).then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
              });
              setTimeout(() => {
                this.reset();
              }, 800);
            });
          })
          .catch(() => {
            
          });
      },
      search() {
        //搜索
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        grouponRecommendLs(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            records.forEach((item) => {});
            this.tableData = records;
            this.$refs.table.pageOptions.total = total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      submitForm(formName) { //抖音小程序商品推荐确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.ruleForm)
            this.submitLoading = true
            if (this.editId == null) {
              var params = {
                ...this.ruleForm,
              };
              addGrouponRecommend(params).then((res) => {
                  this.$message({
                    message: "保存成功",
                    type: "success",
                  });
                  setTimeout(() => {
                    this.reset()
                    this.dialogVisible = false
                  }, 800);
                })
                .finally(() => {
                  setTimeout(() => {
                    this.dialogVisible = false
                  }, 1000);
                });
            } else {
              var params = {
                ...this.ruleForm,
                id: this.editId
              };
              editGrouponRecommend(params).then((res) => {
                  this.$message({
                    message: "修改成功",
                    type: "success",
                  });
                  setTimeout(() => {
                    this.reset()
                    this.dialogVisible = false
                  }, 800);
                })
                .finally(() => {
                  setTimeout(() => {
                    this.dialogVisible = false
                  }, 1000);
                });
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 40px;
  }

  .listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    margin-bottom: 20px;
    display: flex;
  }

  .slot_box {
    display: flex;
    align-items: center;
  }

  .avatar_img {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    margin-right: 10px;
  }

  .sumit_button {
    color: #fff !important;
    background: #ff7b35 !important;
    border: 0;
  }

  .sumit_button:hover {
    opacity: 0.9;
  }

  .card_box {
    width: 160px;
    height: 100px;
    background: linear-gradient(112deg,
        #f85936 0%,
        rgba(255, 147, 123, 0.99) 100%);
    border-radius: 4px;
    font-size: 16px;
    font-weight: bold;
    color: #fff;
    text-align: center;
    line-height: 100px;
  }

</style>
